﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Security;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BisinessLogic.Implementations
{
    public class EFUserRepository : IUsersRepository
    {
        private EFDbContext context;

        public EFUserRepository(EFDbContext context)
        {
            this.context = context;
        }

        public IEnumerable<User> GetUsers()
        {
            return context.Users.ToList();
        }

        public User GetUserById(int id)
        {
            return context.Users.FirstOrDefault(x => x.Id == id);
        }

        public User GetUserByLogin(string login)
        {
            return context.Users.FirstOrDefault(x => x.Login == login);
        }

        public MembershipUser GetMembershipUserByLogin(string login)
        {
            User user = context.Users.FirstOrDefault(x => x.Login == login);
            if (user != null)
            {
                return new MembershipUser("CustomMembershipProvider",
                    user.Login,user.Id,user.Email,"",null,true,false,
                    user.CreateDate,DateTime.Now,DateTime.Now,DateTime.Now,DateTime.Now);
            }
            return null;
        }

        public string GetUserLoginByEmail(string email)
        {
            User user = context.Users.FirstOrDefault(x => x.Email == email);
            return user != null ? user.Login : "";
        }

        public void CreateUser(string login, string password, string email, string firstName, string lastName,
            string middleName, int roleId)
        {

            User user = new User
            {
                Login = login,
                Password = password,
                Email = email,
                RoleId = roleId,
                FirstName = firstName,
                LastName = lastName,
                MiddleName = middleName,
                CreateDate = DateTime.Now
            };
            SaveUser(user);
        }

        public bool ValidateUser(string login, string password)
        {
            User user = context.Users.FirstOrDefault(x => x.Login == login);
            return user != null&& user.Password==password;
        }

        public void SaveUser(User user)
        {
            if (user.Id == 0)
            {
                context.Users.Add(user);
            }
            else
            {
                
                var u =GetUserById(user.Id);
                u.LastName = user.LastName;
                u.FirstName = user.FirstName;
                u.MiddleName = user.MiddleName;
                u.Email = user.Email;
                u.Address = user.Address;
                u.Login = user.Login;
                u.Phone = user.Phone;
                u.PostalCode = user.PostalCode;
                u.Password = user.Password;
                u.RoleId = user.RoleId;
                u.State = user.State;
            }
            context.SaveChanges();
        }

        public   void DeleteUserById(int id)
        {
            context.Users.Remove(GetUserById(id));
            context.SaveChanges();
        }


    }
}
